package com.afkt.app.slice;

import com.afkt.app.ResourceTable;
import com.afkt.app.utils.GetInfoUtils;
import com.dev.utils.app.info.DeviceMod;
import com.dev.utils.app.info.DisplayMod;
import com.dev.utils.app.info.MemoryMod;
import com.dev.utils.app.info.SensorMod;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.abilityjet.databinding.DataBinding;
import ohos.aafwk.abilityjet.databinding.DataBindingUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.data.usage.DataUsage;
import ohos.data.usage.MountState;
import ohos.sensor.bean.*;
import ohos.system.DeviceInfo;
import ohos.telephony.RadioInfoManager;
import ohos.telephony.SimInfoManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.function.Consumer;


public class PhoneInfoAbilitySlice extends AbilitySlice {

    //手机分辨率
    private Text Phone_resolution;

    //手机内存
    private Text Phone_memory;

    //可用内存
    private Text Phone_Avail_memory;

    //手机型号
    private Text Phone_model;

    //设备品牌
    private Text Phone_brand;

    //产品名称
    private Text  Phone_name;

    //设备版本号码
    private Text Equipment_VersionNum;

    private Text deviceID;

    private Button goBack;

    private RadioInfoManager tm;

    private SimInfoManager sm;

    private DisplayMod displayMod;

    private SensorMod sensorMod;

    private DeviceMod deviceMod;

    private MemoryMod memoryMod;



    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_phone_info);
        tm=RadioInfoManager.getInstance(getContext());
        sm = SimInfoManager.getInstance(getContext());
        sensorMod = new SensorMod(this);
        deviceMod = new DeviceMod(this);
        displayMod = new DisplayMod(this);
        memoryMod = new MemoryMod(this);
        ArrayList list = (ArrayList) sensorMod.getAllSensors();
        list.forEach(o -> {
            if(o instanceof CategoryOther){
                CategoryOther sensorData = (CategoryOther) o;

            }else if(o instanceof CategoryOrientation){
                CategoryOrientation sensorData = (CategoryOrientation) o;
                ((Text) findComponentById(ResourceTable.Id_button_2)).setText(sensorData.getVendor()+"");
                ((Text) findComponentById(ResourceTable.Id_button_3)).setText(sensorData.getVendor()+"");

            }else if(o instanceof CategoryMotion){
                CategoryMotion sensorData = (CategoryMotion) o;

            }else if(o instanceof CategoryLight){
                CategoryLight sensorData = (CategoryLight) o;

            }else if(o instanceof CategoryEnvironment){
                CategoryEnvironment sensorData = (CategoryEnvironment) o;

            }else if(o instanceof CategoryBody){
                CategoryBody sensorData = (CategoryBody) o;

            }
        });
        //返回图标
        goBack =(Button) findComponentById(ResourceTable.Id_btn_back_Phone);
        ((Text) findComponentById(ResourceTable.Id_button_1)).setText(DeviceInfo.getModel()+"");
        ((Text) findComponentById(ResourceTable.Id_button_13)).setText(DeviceInfo.getName()+"");
        ((Text) findComponentById(ResourceTable.Id_button_4)).setText(deviceMod.getBuildVersionSDK()+"");
        ((Text) findComponentById(ResourceTable.Id_button_5)).setText(displayMod.getPhysicalSize() +"");
        //获取分辨率
        Phone_resolution = (Text) findComponentById(ResourceTable.Id_button_6);
        String resolution =  Integer.toString(GetInfoUtils.getheight(getContext()))+"x"+Integer.toString(GetInfoUtils.getDisplayWidthInPx(getContext()));
        Phone_resolution.setText(resolution);

        //获取手机内存
        Phone_memory = (Text) findComponentById(ResourceTable.Id_button_9);
        Phone_memory.setText(GetInfoUtils.getPhoneMemory(getAbilityManager()));

        //获取手机可用内存
        Phone_Avail_memory = (Text) findComponentById(ResourceTable.Id_button_10);
        Phone_Avail_memory.setText(GetInfoUtils.getPhoneAvailMemory(getAbilityManager()));

        //获取手机型号
        Phone_model = (Text) findComponentById(ResourceTable.Id_button_1);
        Phone_model.setText(DeviceInfo.getModel());

        //获取设备版本号码
        Equipment_VersionNum = (Text) findComponentById(ResourceTable.Id_button_11);
        Equipment_VersionNum.setText("HUAWEI"+DeviceInfo.getModel());


        //获取产品名称
        Phone_name = (Text) findComponentById(ResourceTable.Id_button_14);
        Phone_name.setText(DeviceInfo.getName());
        ((Text) findComponentById(ResourceTable.Id_button_7)).setText((DataUsage.getDiskMountedStatus() == MountState.DISK_MOUNTED && getExternalCacheDir() != null)?"有":"无");
        ((Text) findComponentById(ResourceTable.Id_button_8)).setText(String.valueOf(memoryMod.convertToGb(memoryMod.getAvailableExternalMemorySize()))
                + " Gb");
        ((Text) findComponentById(ResourceTable.Id_button_81)).setText(String.valueOf(memoryMod.convertToGb(memoryMod.getTotalExternalMemorySize()))
                + " Gb");
        ((Text) findComponentById(ResourceTable.Id_button_9)).setText(String.valueOf(memoryMod.convertToGb(memoryMod.getTotalInternalMemorySize()))
                + " Gb");
        ((Text) findComponentById(ResourceTable.Id_button_10)).setText(String.valueOf(memoryMod.convertToGb(memoryMod.getAvailableInternalMemorySize()))
                + " Gb");


    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
